package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBookList =  addressBookService.list();
        return Result.success(addressBookList);
    }
    @GetMapping("/default")
    public Result<AddressBook> defaultAddressBook(){
        AddressBook addressBook = addressBookService.defaultAddreddBook();
        return Result.success(addressBook);
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookById(@PathVariable Integer id){
        AddressBook addressBook =  addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteAddressBookById(Integer id){
        addressBookService.deleteAddressBood(id);
        return Result.success();
    }

    @PutMapping("/default")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook){
        System.out.println("addressBook="+addressBook);
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }
}
